package com.longmen.controller;

import com.longmen.pojo.entity.User;
import com.longmen.pojo.vo.UserLoginVo;
import com.longmen.result.Result;
import com.longmen.service.UserService;
import com.longmen.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户相关接口
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User user1 = userService.login(user);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userid", user1.getId());
        String token = JwtUtil.createJWT("key", 7200000, claims);

        UserLoginVo tbUserVo = new UserLoginVo();
        BeanUtils.copyProperties(user1, tbUserVo);
        tbUserVo.setToken(token);

        return Result.success(tbUserVo);
    }

}
